package jdbc;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String host=System.getProperty("url","jdbc:laud://192.168.3.1:9170?data_port=9091&support_function=true&hive=false");
		Class.forName("cn.edu.thu.laud.jdbc.LaUDDriver");
		Connection con = DriverManager.getConnection(host, "user1", "pwd1");
		Statement statement=con.createStatement();
		try{
			statement.execute("drop keyspace  Keyspace2 ;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		try{
			statement.execute("create keyspace  Keyspace2 with  strategy_class ='org.apache.cassandra.locator.SimpleStrategy' and strategy_options:replication_factor=1;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		statement.execute("use Keyspace2");
		try{
			statement.execute("create columnfamily Standard1 (id varchar primary key,age int,name varchar,nick varchar,imagefeature blob,hphoto 'hblob',sphoto 'sblob',lphoto 'lblob')");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		//		try{
		//			statement.execute("create index user1_nick_idx on Standard1(nick)"); 
		//		}catch (SQLException e) {
		//			System.err.println(e.getMessage());
		//		}
		//		try{
		//			statement.execute("create index user1_age_idx on Standard1(age) "); 
		//			
		//		}catch (SQLException e) {
		//			System.err.println(e.getMessage());
		//		}
		//		try{
		//			statement.execute("create index  on Standard1(name) with " +
		//					"'class_name'='cn.edu.thu.laud.lasql.index.LaUDIndex' "+
		//					"and 'implemention_class'='cn.edu.thu.laud.lasql.index.iface.impl.test.TestIndex' "+
		//					"and  'test'='value1' and 'param2'='value2'"); 
		//		}catch (SQLException e) {
		//			System.err.println(e.getMessage());
		//		}
		//		try{
		//			statement.execute("create index on user1(name) " +
		//			"with " 
		//			+"'class_name'='cn.edu.thu.laud.lasql.index.LaUDIndex'" 
		//			+"and 'implemention_class'='com.trs.laudms.index.TRSIndex'" 
		//			+"and 'host'='192.168.3.29' and 'port'='8888'"
		//			);
		//		}catch (SQLException e) {
		//			System.err.println(e.getMessage());
		//		}
	
		
//		try{
//			statement.execute("create index  on Standard1(name) "); 	
//		}catch (SQLException e) {
//			System.err.println(e.getMessage());
//		}
		ResultSet rsResultSet;

	//	statement.execute("delete from Standard1 where id=1");


		//should make sure that the file is existed.
		//		statement.execute("insert into Standard1(id,name,hphoto,sphoto,lphoto) values (300,'yiguolei','g:/test/1.jpg','g:/test/6.jpg','g:/test/UBUNTU.png')"); //,'F:/lib/laud-thrift-1.0.7-1.jar'
		//		//statement.execute("update Standard1 set hphoto='g:/test/1.jpg' where id=100");
		//		rsResultSet = statement.getResultSet();
		//		while(rsResultSet.next()){
		//			rsResultSet.updateBlob("hphoto", System.in);
		//			rsResultSet.updateBlob("sphoto", System.in);
		//			rsResultSet.updateBlob("lphoto", System.in);
		//		}
		//		rsResultSet.close();
		//	
		//		
		//		
		//		
//				statement.execute("insert into Standard1(id,name,hphoto) values (4,'yiguolei',null)"); //,'F:/lib/laud-thrift-1.0.7-1.jar'
//				rsResultSet = statement.getResultSet();
//				while(rsResultSet.next()){
//					//rsResultSet.updateBlob("hphoto", System.in);
//					//rsResultSet.updateBlob("sphoto", System.in);
//					//rsResultSet.updateBlob("lphoto", System.in);
//				}
//				rsResultSet.close();

		//statement.execute("insert into Standard1(id ) values(102)");
		//for(int i=0;i<100;i++){
			statement.execute("insert into Standard1(id,name,hphoto) values (101,'yiguolei5','g:/jdk-6u27-windows-x64.exe')"); //,'F:/lib/laud-thrift-1.0.7-1.jar'
			rsResultSet = statement.getResultSet();
			while(rsResultSet.next()){
				rsResultSet.updateBlob("hphoto", System.in);
				//rsResultSet.updateBlob("sphoto", System.in);
				//rsResultSet.updateBlob("lphoto", System.in);
			}
			rsResultSet.close();
		//}


		statement.execute("select * from Standard1 where id=101");
		rsResultSet = statement.getResultSet();
		int i=0;
		while(rsResultSet.next()){
			System.out.println(rsResultSet.getObject(1));
			System.out.println(rsResultSet.getObject(2));
			
//			System.out.println(rsResultSet.getObject("id"));
//			System.out.println(rsResultSet.getObject("name"));
//			//hblob ,lblob .we will save them automaticly..
//			//rsResultSet.getBlob("hphoto");
//			//rsResultSet.getBlob("lphoto");
//
//			//((FileTransfor)rsResultSet).getBlob("lphoto","g:/result/");
//			((FileTransfor)rsResultSet).getBlob("hphoto","g:/result/"+i);
//			//System.out.println("lphoto:"+rsResultSet.getString("lphoto"));
//			System.out.println("hphoto:"+rsResultSet.getString("hphoto"));
//			/*
//					//if we want to save in a designed location.we should do like this:
//					 ((FileTransfer)rsResultSet).getBlob("photo","e:/test/");
//			 */
//			//with sblob,we do not save them in file . we return a java.sql.Blob object.
//
////			Blob blob=rsResultSet.getBlob("sphoto");
////			//we can save this blob as a file like this:
////			if(blob!=null){
////				InputStream iStream=blob.getBinaryStream();
////				writeIntoFile(iStream, "g:/result/abc"+i);
////			}
			i++;
			System.out.println("------------------------------");
		}
		//remeber close ResultSet !!!!!!
		rsResultSet.close();
		statement.close();
	}
	private static  void writeIntoFile(InputStream instream,String filePath) throws IOException, SQLException{
		File file=new File(filePath);

		if(file.isDirectory()){
			throw new SQLException("file not folder..");
		}
		BufferedInputStream in = new BufferedInputStream(instream);        
		FileOutputStream out = new FileOutputStream(file);        

		//  System.out.println("Available bytes:" + in.available());        

		byte[] temp = new byte[4096];        
		int size = 0;        
		try{
			while ((size = in.read(temp)) != -1) {        
				out.write(temp, 0, size);        
			}
		}catch (IOException e) {
			throw e;
		}
		finally{
			try {
				in.close();
			} catch (IOException e) {
				in=null;
			}        
		}
		in.close();
		out.close();
	}
}
